Repository navigation
Use the screened short-needle search for find, contains, and rfind - #188
Merged
Merged
Conversation
The setup-free screened search that split_str uses moves to search.rs as find_short, gains a reverse twin, rfind_short, and now serves find and rfind for 2- to 32-byte needles in haystacks under 256 bytes, and contains where std's SIMD contains cannot run. Longer haystacks keep memmem for find and str::rfind for rfind; the str::find arm, unreachable under the new rule, is removed. After four false candidates each search hands the text it has not ruled out to a Two-Way searcher; the rfind hand-off cuts the text at the first character boundary at or after the last possible match end. The screened searches are called out of line from the dispatch: inlined, they cost the other arms 2 to 6 ns per call. On an i7-11700K find takes 0.59 and rfind 0.52 of their previous time across the threshold sweep, contains is unchanged overall. Differential tests cover every offset across block boundaries and the 256-byte limit, and the hand-offs with ASCII and multi-byte needles, including a match that ends exactly at the reverse cut. Closes #186 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The instruction-count gate failed contains/32B_miss, 138 -> 151 instructions, although that input takes std's SIMD arm. After the out-of-line screen call, contains_str still matched on its result and could call the hand-off, so the inputs stayed live across the call and the function saved and restored four registers on every call, whatever the arm. contains_screened, find_screened, and rfind_screened now each run the screen and its hand-off and return the final answer, so every dispatch arm is a single call. contains_str's prologue matches main again; the same-binary A/B keeps every unchanged path within 5% of main. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The instruction-count gate still measured contains/32B_miss, which takes std's SIMD arm, at 145 instructions against 138 on main. With the screen as a separate out-of-line arm next to the inline memmem call, the inlined contains_str compiled differently around the std arm. contains_str now keeps main's arms up to and including the std arm, and sends every other input to contains_without_std_simd, which picks between the screen and memmem out of line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The gate still measured contains/32B_miss at 145 instructions against 138. Its scenario code was identical; the difference was in std_contains. The screened hand-off called str::contains directly, a second call site, so LLVM stopped inlining core's search into std_contains and turned it into a seven-instruction shuffle and jump to a shared copy. The contains and rfind hand-offs now call std_contains and std_rfind, so each std search keeps one call site. In the harness build, std_contains and std_rfind compile to the same 297 and 283 instructions as on main. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the last review finding in #186: the setup-free screened search only served
split_str, whilefind,contains, andrfindpaid searcher setup for the same short needles.Summary
find_short,ShortSearch, and their constants move frompattern.rstosearch.rs, andsplit_strimports them.rfind_shortis the same screen scanning blocks from the end, taking the highest candidate firstfindandrfindscreen haystacks under 256 bytescontainsscreens only where std's SIMDcontainscannot run: before the haystack holds a 16-byte block plus the needle, or under 256 bytes on targets without that fast pathmemmemforfindandstr::rfindforrfindstr::findarm (16 + 3n <= L < 64) can no longer be reached, so it and its constants are removedfindandcontainscontinue from the last false candidate, which holds the needle's first byte and so is a character boundaryrfindcuts the text at the first character boundary at or afterlast candidate + needle - 1. The up to three extra bytes cannot hold a match start, because those positions were already ruled outcontains_screened,find_screened,rfind_screened) that runs the screen and its hand-off and returns the final answer. With the screen inlined, or with the dispatch matching on its result, the other arms paid 2 to 6 ns or saved and restored four registers on every call.split_strstill inlinesfind_shortcontains/find/rfinddocs, README search section, and the CHANGELOG search entry describe the new rulesMeasured impact
benches/search_thresholds.rson an i7-11700K,CheetahStringtimes compared withmainover every input of the sweep (geometric mean):strandmemmemfindrfindcontainsExamples, ns per call:
mainfind, 8-byte needle, 32 B, missfind, 16-byte needle, 64 B, missfind, 32-byte needle, 64 B, missrfind, 16-byte needle, 64 B, missrfind, 4-byte needle, 128 B, misscontains, 32-byte needle, 40 B, missCosts:
findandcontains; they never fill a screening block and take the scalar tailrfind512 B–4 KiB) and within 8% (find512 B, which moved between 1.01x and 1.15x across runs).containson the std path was 0.2–0.4 ns slower in the A/BVariants measured and dropped:
findon 4 KiB with a 2-byte needle went from 90 to 250 ns, andcontainsfrom std's SIMD path got up to 2.9x slowercontainson 128 B took 2x as longCI failure and fix
The instruction-count gate failed
contains/32B_miss, an input that takes std's SIMD arm and never reaches the screen. It took three steps to fix:contains_strmatched on the out-of-line screen's result and could call the hand-off. That kept its inputs live across the call, so it saved and restored four registers on every call. Fix: each screened arm became one call that returns the final answer.main; I checked by building the gate harness with--emit asmfor both sides. The difference was insidestd_contains. The screened hand-off calledstr::containsdirectly, a second call site, so LLVM stopped inlining core's search intostd_contains. That made it a seven-instruction shuffle and jump to a shared copy.main. Thecontainsandrfindhand-offs now call thestd_containsandstd_rfindwrappers, so each std search keeps one call site. In the harness build,std_containsandstd_rfindcompile to the same 297 and 283 instructions as onmain.Final gate, instructions per call:
contains/32B_missfind/48B_missrfind/48B_missfind/1KiB_missCI sweeps
Search thresholdson the final commit.CheetahStringtime against the faster ofstrandmemmemin the same run, geometric mean over the sweep. This is compared within one run because hosted runners differ from run to run:ubuntu-latestbeforeubuntu-24.04-armbeforefindrfindcontainsOn aarch64, a 2-byte needle in a 192-byte haystack is the weakest point: 26.8 to 34.3 ns, where NEON
memmembeats the screen near the 256-byte limit.Verification
cargo fmt --check; clippy-D warningsfor all targets with all features, no default features, andserde,bytes,simdcargo test --all-features: 248 passed, 2 of them new:--no-default-features --lib: 17 passed; doctests: 38 passedcargo +1.95.0 check --all-features; Python tests passrfindhand-off text one byte short fails the hand-off test, through a case whose last match ends exactly at the cutSearch thresholdsrun measures the new dispatch on x86_64 and aarch64Closes #186
🤖 Generated with Claude Code